package com.konsung.ui.defineview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.konsung.healthfile.R;
import com.konsung.util.BroadcastUtils;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 弹出对话框
 */
public class PopupDialog extends Dialog {
    @BindView(R.id.rl_root_view)
    RelativeLayout rlRootView;
    @BindView(R.id.ll_dialog)
    LinearLayout llDialog;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.btn_cancel)
    Button btnCancel;
    @BindView(R.id.btn_delete)
    Button btnDelete;
    private Context context;
    //弹出框标题
    private String title = "";
    //弹出框是否弹出标志位
    public boolean isShow = false;
    //按钮状态
    private UpdateButtonState updateButtonState;
    public static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime = 0;

    /**
     * 事件回调
     */
    public interface UpdateButtonState {
        /**
         * 获取确认、取消
         * @param pressed 确认、取消
         */
        void getButton(Boolean pressed);

        /**
         * 关闭弹窗
         */
        void close();
    }

    /**
     * 构造器
     * @param context context
     * @param title context
     * @param updateButtonState 回调
     */
    public PopupDialog(Context context, String title, UpdateButtonState
            updateButtonState) {
        super(context, android.R.style.Theme_Translucent);
        this.context = context;
        this.title = title;
        this.updateButtonState = updateButtonState;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_delete);
        ButterKnife.bind(this);

        btnDelete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                long currentTime = Calendar.getInstance().getTimeInMillis();
                if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                    updateButtonState.getButton(true);
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long currentTime = Calendar.getInstance().getTimeInMillis();
                if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                    lastClickTime = currentTime;
                    updateButtonState.getButton(false);
                }
            }
        });
        tvTitle.setText(title);
        ivClose.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (isShow) {
                    return;
                }
                long currentTime = Calendar.getInstance().getTimeInMillis();
                if (currentTime - lastClickTime > MIN_CLICK_DELAY_TIME) {
                    lastClickTime = currentTime;
                    updateButtonState.close();
                }
                isShow = true;
                btnClose();
                hide();
            }
        });
    }

    @Override
    public void show() {
        super.show();
        BroadcastUtils.sendDarkForStartManage();
        // set dialog enter animations
//        llDialog.startAnimation(AnimationUtils.loadAnimation(context, R
//                .anim.dialog_main_show_amination));
//        rlRootView.startAnimation(AnimationUtils.loadAnimation(context, R.anim
//                .dialog_root_show_amin));
    }

    @Override
    public void hide() {
        super.hide();
        isShow = false;
        BroadcastUtils.sendNormalForStartManage();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        isShow = false;
        BroadcastUtils.sendNormalForStartManage();

    }

    /**
     * 设置标题
     * @param title 标题
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 设置内容
     * @param content 主内容
     */
    public void setContent(String content) {
        this.tvContent.setText(content);
    }

    /**
     * 设置按钮内容
     * @param button 按钮内容
     */
    public void setButton(String button) {
        this.btnDelete.setText(button);
    }

    /**
     * 方法空实现，用户点击关闭的实现
     */
    public void btnClose() {

    }

    // ### 隐藏关闭图标，有些提示信息中不需要看到关闭的那个图标

    /**
     * 设置是否隐藏tipsdialog中的关闭按钮
     * <p/>
     * flag:
     * true  隐藏
     * false 不隐藏
     *
     * @param flag 是否隐藏
     */
    public void setHideIvClose(boolean flag) {
        if (!flag) {
            ivClose.setVisibility(View.VISIBLE);
        } else {
            ivClose.setVisibility(View.GONE);
        }
    }
}
